﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthOnline.Business;
using MyHealthOnline.Dal.Generated.EntityClasses;
using MyHealthOnline.Dal.Generated.CollectionClasses;

namespace MyHealthOnline.Specialist
{
    public partial class ConsultHistory : System.Web.UI.UserControl
    {
       
        public void SetDataSource(QuestionCollection questionColl)
        {
            rptMessageHistory.DataSource = questionColl; //MessageBusiness.GetMessages(ConsultId);
            rptMessageHistory.DataBind();
        }
        
        private void SetupQuestionList(int questionnaireId)
        {
            ucViewQuestionList.DataSource = QuestionBusiness.GetQuestionnaireQuestions(questionnaireId);
        }

        protected void rptMessageHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
            {
                QuestionEntity question = e.Item.DataItem as QuestionEntity;

                Label lblCreationDate = e.Item.FindControl("lblCreationDate") as Label;
                lblCreationDate.Text = question.QuestionDate.ToShortDateString();


                if (question.AnswerDate.HasValue)
                {
                    Label lblReactionDate = e.Item.FindControl("lblReactionDate") as Label;
                    lblReactionDate.Text = question.AnswerDate.Value.ToShortDateString();
                }

                Label lblMessageType = e.Item.FindControl("lblMessageType") as Label;
                lblMessageType.Text = question.Type;

                Label lblMessageText = e.Item.FindControl("lblMessageText") as Label;
                lblMessageText.Text = question.QuestionText;

                Label lblAnswerType = e.Item.FindControl("lblAnswerType") as Label;

                Label lblAnswerText = e.Item.FindControl("lblAnswerText") as Label;
                lblAnswerText.Text = (question.Type == "Open") ?
                                        question.OpenAnswer :
                                        (question.Type == "Multi") ?
                                            question.MultiAnswer :
                                            (question.Type == "Date" && question.DateAnswer.HasValue) ?
                                                question.DateAnswer.Value.ToShortDateString() :
                                                "";

                Button btnViewQuestionnaire = e.Item.FindControl("btnViewQuestionnaire") as Button;

                if (question.Type == "Questionnaire")
                {
                    btnViewQuestionnaire.Click += new EventHandler(btnViewQuestionnaire_Click);
                    btnViewQuestionnaire.CommandArgument = question.Questionnaire.Id.ToString();
                    btnViewQuestionnaire.Visible = true;
                }
                else btnViewQuestionnaire.Visible = false;
            }
        }

        void btnViewQuestionnaire_Click(object sender, EventArgs e)
        {
            Button btn=sender as Button;
            int id = Convert.ToInt32(btn.CommandArgument);
            SetupQuestionList(id);
            pnlQuestionList.Visible = true;
            pnlHistory.Visible = false;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            pnlQuestionList.Visible = false;
            pnlHistory.Visible = true;
        }

        protected void rptMessageHistory_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (e.CommandName == "View")
                {
                    int questionnaireId = Convert.ToInt32(e.CommandArgument);
                    SetupQuestionList(questionnaireId);
                    pnlQuestionList.Visible = true;
                    pnlHistory.Visible = false;
                }
            }
        }

    }
}